<?php




/**
* Lists entities in images
*/

# When a user will cancel add/edit forms, we will try to come back here
url::remember();

# Begin the paginator along with: images
# Parameter: GET[], and per page
$pagination = new pagination('pg', IMAGES_PER_PAGE);

$images = new images();

$search_images = $variable->remember_string('search_images');
$smarty->assign('search_images', $search_images);

$multi_matches = array_map('trim', preg_split('/[\/|,]+/is', $search_images));
$multi_matches_sqls = array();
if(count($multi_matches))
{
	foreach($multi_matches as $mm => $search_term)
	{
		$multi_matches_sqls[] = "(
			e.image_caption LIKE '%{$search_term}%' 
			OR e.image_keywords LIKE '%{$search_term}%' 
			OR e.image_description_short LIKE '%{$search_term}%'
		)";
	}
	# overwrite the variable
	$multi_matches_sqls = implode(' AND ', $multi_matches_sqls);
	$search_images='';
	#die($multi_matches_sqls);
}
#print_r($multi_matches);
#die($search_images);

# Keep a search log for future analysys.
# For now, it may decrease the performance of the search engine.
$active_post = isset($_POST['search_images']);
if($active_post)
{
	$tags = new tags();
	foreach($multi_matches as $mm => $search_term)
	{
		$tags->search_log($search_term);
	}
}

$condition = new condition();

$condition->add('FULL', array(
	"e.subdomain_id={$subdomain_id}", # Bind the records
	"e.is_active='Y'", # Do not remove this
	#"e.is_approved='Y'", # Optionally use this flag
	($search_images)?"(
		e.image_caption LIKE '%{$search_images}%' 
		OR e.image_keywords LIKE '%{$search_images}%' 
		OR e.image_description_short LIKE '%{$search_images}%'
	 	OR e.image_description LIKE '%{$search_images}%'
	 )":'',
	$multi_matches_sqls?$multi_matches_sqls:'',
));

# Compulsory conditions
$condition->add('AND', array(
	#'e.search_field' => 'Y', # Partial %contents%
));

# List out the entries
$entries = $images->list_entries(
	$condition,
	$from_index=$pagination->begining_entry(),
	$pagination->per_page()
);

# Pagination helper
$pagination->set_total($images->total_entries());
$smarty->assignByRef('pagination', $pagination);

# Assign to Smarty: Lists
$smarty->assignByRef('imagess', $entries);
?>